﻿<%@ WebHandler Language="C#" Class="ResHandler" %>

using System;
using System.Web;
using System.Web.SessionState;

public class ResHandler : IHttpHandler,IRequiresSessionState {
    
    public void ProcessRequest (HttpContext context) {
        string action = context.Request.Params["act"];
        if (action == "save")
        {
            SaveRes(context);
        }
        else if (action == "update")
        {
            Update(context);
        }
        else if (action == "getres")
        {
            GetRes(context);
        }
        else if (action == "addatt")
        {
            AddAtt(context);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    private void SaveRes(HttpContext context)
    {
        int resid = 0;
        Cstudy.MODEL.resources res = new Cstudy.MODEL.resources();
        res.Title = context.Request.Params["title"];
        res.Content = context.Request.Params["content"];
        try
        {
            res.ClassID = int.Parse(context.Request.Params["classid"]);
        }
        catch
        {
            res.ClassID = 0; 
        }
        res.CreateDate = DateTime.Now;

        res.AuthorID = (int)HttpContext.Current.Session["userid"];
        
        Cstudy.BLL.resources bllres = new Cstudy.BLL.resources();
        if (bllres.AddRescources(res, out resid))
        {
            context.Response.Write(resid);
        }
        else
        {
            context.Response.Write(0); 
        }
    }

    private void Update(HttpContext context)
    {
        int resid = int.Parse(context.Request.Params["resid"]);
        Cstudy.MODEL.resources res = new Cstudy.MODEL.resources();
        res.ResID = resid;
        res.Title = context.Request.Params["title"];
        res.Content = context.Request.Params["content"];
        try
        {
            res.ClassID = int.Parse(context.Request.Params["classid"]);
        }
        catch
        {
            res.ClassID = 0;
        }

        res.AuthorID = (int)HttpContext.Current.Session["userid"];

        Cstudy.BLL.resources bllres = new Cstudy.BLL.resources();
        if (bllres.UpdateRescoures(res))
        {
            context.Response.Write(resid);
        }
        else
        {
            context.Response.Write(0);
        }
    }

    private void GetRes(HttpContext context)
    {
        Cstudy.MODEL.resources res = new Cstudy.MODEL.resources();
        int resid = int.Parse(context.Request.Params["resid"]);
        System.Data.DataSet ds = new Cstudy.BLL.resources().SelectResourceByResID(resid, false);
        res = Cstudy.MODEL.resources.Parse(ds);
        context.Response.ContentType = "application/json";
        context.Response.Write(Cstudy.MODEL.resources.ToJson(res));
        context.Response.End();
    }

    private void AddAtt(HttpContext context)
    {
        Cstudy.MODEL.attachment att = new Cstudy.MODEL.attachment();
        att.ResID = int.Parse(context.Request.Params["resid"]);
        try
        {
            att.Size = int.Parse(context.Request.Params["size"]);
        }
        finally
        {
            att.Size = 0; 
        }
        att.Name = context.Request.Params["filename"];
        att.Url = context.Request.Params["fileurl"];
        att.Descscription = context.Request.Params["filename"];
        if (new Cstudy.BLL.attachment().AddAttachment(att))
        {
            context.Response.Write(1);
        }
        else
        {
            context.Response.Write(0); 
        }
    }
}